/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Employee.java
 *
 * Created on Oct 29, 2011, 3:44:20 PM
 */
package VIMS;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;


class MyPhotoFilter extends javax.swing.filechooser.FileFilter 
{
        @Override
        public boolean accept(File file) {
            // Allow only directories, or files with ".jpg" extension
            return file.isDirectory() || file.getAbsolutePath().endsWith(".jpg");
        }
        @Override
        public String getDescription() {
            // This description will be displayed in the dialog,
            // hard-coded = ugly, should be done via I18N
            return "Text documents (*.jpg)";
        }
 }
/**
 *
 * @author shyan
 */
public class Employee extends javax.swing.JFrame {

    private Database dbOB = new Database();
    private ResultSet rset = null;
    private String action ="";
    private String selectedID=null;
    private String PATH =null;
    
    /** Creates new form Employee */
    public Employee(){
        super("Add New Product");
        initComponents();
        setLocationRelativeTo(null);
        
        FileChooser.setVisible(false);
        NextEmpID();
        FormattedField();
        GroupButton();
        
    }
    
    public Employee(String choice,String selectedRowID) 
    {
        super(choice+" product");
        initComponents();
        setLocationRelativeTo(null);  
        action=choice;
        selectedID=selectedRowID;
        FileChooser.setVisible(false);
        
        if(action.equals("Add"))
        {
            EmpButton.setText(action);
            NextEmpID();
            FormattedField();
            GroupButton();
        }
        if(action.equals("Delete"))
        {
            EmpButton.setText(action);
            ComponentDISABLED();
            DisplayEmpInfo();
            GroupButton();
        }
        if(action.equals("View"))
        {
            EmpButton.setText("Edit");
            Position.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"Planning Manager", "Head of Logistic", "Clerk", "Administrator" }));
            ComponentDISABLED();
            DisplayEmpInfo();
            GroupButton();
        }
        
        
    }

    // Set IC, Contact and Post Code as Formatted Field
    private void FormattedField()
    {
        try 
        {   MaskFormatter fmtr1 = new MaskFormatter("######-##-####");   
            fmtr1.setPlaceholderCharacter('_');   
            fmtr1.setValueContainsLiteralCharacters(false);   
            IC.setFormatterFactory(new DefaultFormatterFactory(fmtr1));  
            
            MaskFormatter fmtr2 = new MaskFormatter("###-#######");   
            fmtr2.setPlaceholderCharacter('_');   
            fmtr2.setValueContainsLiteralCharacters(false);
            Contact.setFormatterFactory(new DefaultFormatterFactory(fmtr2));  
            
            MaskFormatter fmtr3 = new MaskFormatter("#####");   
            fmtr3.setValueContainsLiteralCharacters(false);
            PostCode.setFormatterFactory(new DefaultFormatterFactory(fmtr3)); 
        } 
        catch (java.text.ParseException ex) 
        {   
            ex.printStackTrace();
        }
    }
    
    //Group radio button
    private void GroupButton()
    {
        ButtonGroup button = new ButtonGroup();
        button.add(active);
        button.add(inactive);
    }
    
    // Return Selected Radio Button
    private String SelectedButton()
    {
        if(active.isSelected())
            return "ACTIVE";
        if(inactive.isSelected())
            return "INACTIVE";
        return "";
    }
    
   //Retrive Next New Employee ID
    private void NextEmpID() 
    { 
        int currentNewEnum=0;
        Database ob = new Database();
        ResultSet rset = ob.getResultSet("SELECT MAX(EMP_ID) FROM employee");
        try{
            if(rset.next())
                currentNewEnum=rset.getInt(1)+1;
           }
        catch(SQLException ex)
           {
             ex.printStackTrace();
           }
        IdField.setText(Integer.toString(currentNewEnum));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        Photo = new javax.swing.JLabel();
        IdField = new javax.swing.JLabel();
        UidLabel = new javax.swing.JLabel();
        PosLabel = new javax.swing.JLabel();
        UserName = new javax.swing.JTextField();
        UnLabel = new javax.swing.JLabel();
        FnLabel = new javax.swing.JLabel();
        FirstName = new javax.swing.JTextField();
        LnLabel = new javax.swing.JLabel();
        LastName = new javax.swing.JTextField();
        IcLabel = new javax.swing.JLabel();
        IC = new javax.swing.JFormattedTextField();
        Gender = new javax.swing.JComboBox();
        GenderLabel = new javax.swing.JLabel();
        RaceLabel = new javax.swing.JLabel();
        Race = new javax.swing.JComboBox();
        Religion = new javax.swing.JComboBox();
        ReligionLabel = new javax.swing.JLabel();
        Email = new javax.swing.JTextField();
        EmailLabel = new javax.swing.JLabel();
        Contact = new javax.swing.JFormattedTextField();
        ConLabel = new javax.swing.JLabel();
        City = new javax.swing.JTextField();
        CityLabel = new javax.swing.JLabel();
        PostCode = new javax.swing.JFormattedTextField();
        PcLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Address = new javax.swing.JEditorPane();
        AddLabel = new javax.swing.JLabel();
        EmpButton = new javax.swing.JButton();
        CloseEmpButton = new javax.swing.JButton();
        Position = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        StatusLabel = new javax.swing.JLabel();
        inactive = new javax.swing.JRadioButton();
        active = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        FileChooser = new javax.swing.JFileChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));
        setResizable(false);

        jLayeredPane1.setMaximumSize(new java.awt.Dimension(800, 600));
        jLayeredPane1.setMinimumSize(new java.awt.Dimension(800, 600));

        Photo.setFont(new java.awt.Font("Times New Roman", 3, 12));
        Photo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Photo.setText("Click here to add photo");
        Photo.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        Photo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                PhotoMouseClicked(evt);
            }
        });
        Photo.setBounds(330, 10, 170, 170);
        jLayeredPane1.add(Photo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        IdField.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        IdField.setBounds(170, 210, 150, 30);
        jLayeredPane1.add(IdField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        UidLabel.setText("User ID :");
        UidLabel.setBounds(80, 210, 80, 30);
        jLayeredPane1.add(UidLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        PosLabel.setText("Position :");
        PosLabel.setBounds(80, 250, 80, 30);
        jLayeredPane1.add(PosLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        UserName.setBounds(170, 290, 150, 30);
        jLayeredPane1.add(UserName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        UnLabel.setText("User name :");
        UnLabel.setBounds(80, 290, 80, 30);
        jLayeredPane1.add(UnLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FnLabel.setText("First name :");
        FnLabel.setBounds(80, 330, 80, 30);
        jLayeredPane1.add(FnLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        FirstName.setBounds(170, 330, 150, 30);
        jLayeredPane1.add(FirstName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        LnLabel.setText("Last name :");
        LnLabel.setBounds(80, 370, 80, 30);
        jLayeredPane1.add(LnLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        LastName.setBounds(170, 370, 150, 30);
        jLayeredPane1.add(LastName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        IcLabel.setText("IC no.       :");
        IcLabel.setBounds(80, 410, 80, 30);
        jLayeredPane1.add(IcLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        IC.setBounds(170, 410, 150, 30);
        jLayeredPane1.add(IC, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Gender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose", "Male", "Female" }));
        Gender.setBounds(170, 450, 80, 30);
        jLayeredPane1.add(Gender, javax.swing.JLayeredPane.DEFAULT_LAYER);

        GenderLabel.setText("Gender    :");
        GenderLabel.setBounds(80, 450, 80, 30);
        jLayeredPane1.add(GenderLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        RaceLabel.setText("Race        :");
        RaceLabel.setBounds(80, 490, 80, 30);
        jLayeredPane1.add(RaceLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Race.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose", "Chinese", "Malay", "Indian", "Others" }));
        Race.setBounds(170, 490, 80, 30);
        jLayeredPane1.add(Race, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Religion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose", "Buddha", "Islam", "Hindu", "Christian", "Other" }));
        Religion.setBounds(170, 530, 80, 30);
        jLayeredPane1.add(Religion, javax.swing.JLayeredPane.DEFAULT_LAYER);

        ReligionLabel.setText("Religion    :");
        ReligionLabel.setBounds(80, 530, 80, 30);
        jLayeredPane1.add(ReligionLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        Email.setBounds(530, 210, 150, 30);
        jLayeredPane1.add(Email, javax.swing.JLayeredPane.DEFAULT_LAYER);

        EmailLabel.setText("Email         :");
        EmailLabel.setBounds(450, 210, 70, 30);
        jLayeredPane1.add(EmailLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        Contact.setBounds(530, 250, 150, 30);
        jLayeredPane1.add(Contact, javax.swing.JLayeredPane.DEFAULT_LAYER);

        ConLabel.setText("Contact    :");
        ConLabel.setBounds(450, 250, 70, 30);
        jLayeredPane1.add(ConLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        City.setBounds(530, 290, 150, 30);
        jLayeredPane1.add(City, javax.swing.JLayeredPane.DEFAULT_LAYER);

        CityLabel.setText("City          :");
        CityLabel.setBounds(450, 290, 70, 30);
        jLayeredPane1.add(CityLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        PostCode.setBounds(530, 330, 150, 30);
        jLayeredPane1.add(PostCode, javax.swing.JLayeredPane.DEFAULT_LAYER);

        PcLabel.setText("Postcode  :");
        PcLabel.setBounds(450, 330, 70, 30);
        jLayeredPane1.add(PcLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jScrollPane1.setViewportView(Address);

        jScrollPane1.setBounds(530, 370, 150, 120);
        jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        AddLabel.setText("Address    :");
        AddLabel.setBounds(450, 370, 70, 30);
        jLayeredPane1.add(AddLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        EmpButton.setText("Add");
        EmpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EmpButtonActionPerformed(evt);
            }
        });
        EmpButton.setBounds(580, 570, 80, 30);
        jLayeredPane1.add(EmpButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        CloseEmpButton.setText("Cancel");
        CloseEmpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseEmpButtonActionPerformed(evt);
            }
        });
        CloseEmpButton.setBounds(660, 570, 80, 30);
        jLayeredPane1.add(CloseEmpButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Position.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose", "Planning Manager", "Head of Logistic", "Clerk", "Administrator" }));
        Position.setBounds(170, 250, 150, 30);
        jLayeredPane1.add(Position, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator1.setBounds(410, 200, 10, 360);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        StatusLabel.setText("Status   :");
        StatusLabel.setBounds(450, 500, 60, 30);
        jLayeredPane1.add(StatusLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        inactive.setText("INACTIVE");
        inactive.setBounds(610, 500, 80, 30);
        jLayeredPane1.add(inactive, javax.swing.JLayeredPane.DEFAULT_LAYER);

        active.setText("ACTIVE");
        active.setBounds(530, 500, 70, 30);
        jLayeredPane1.add(active, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Employee's Personal Details :"));
        jLabel1.setBounds(60, 180, 690, 390);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FileChooser.setCurrentDirectory(new java.io.File("C:\\Users\\shyan\\Documents\\NetBeansProjects\\CAT300Project_____GOOGLE CODE\\src\\Photos"));
        FileChooser.setFileFilter(new MyPhotoFilter());
        FileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FileChooserActionPerformed(evt);
            }
        });
        FileChooser.setBounds(50, 20, 270, 150);
        jLayeredPane1.add(FileChooser, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 621, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CloseEmpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseEmpButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_CloseEmpButtonActionPerformed

    private void EmpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EmpButtonActionPerformed
        if(EmpButton.getText().equals("Delete"))
        {
            inactive.setSelected(true);
            DeleteEmployee();
        }
        else if(EmpButton.getText().equals("Edit"))
        {
            EmpButton.setText("Save");
            Position.setEnabled(true);
        }
        else if(EmpButton.getText().equals("Save"))
        {
            UpdateEmployee();
        }
        else{ 
            AddEmployee();
//            try {
//                StoreImage();
//            } catch (FileNotFoundException ex) {
//                Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (SQLException ex) {
//                Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (IOException ex) {
//                Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (ClassNotFoundException ex) {
//                Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
//            }
        }
    }//GEN-LAST:event_EmpButtonActionPerformed

    private void AddEmployee()
    {
        String EMP_ID       =IdField.getText();
        String EMP_JOBTITLE =Position.getSelectedItem().toString();
        String EMP_USERNAME =UserName.getText().trim();
        String EMP_FNAME    =FirstName.getText().trim();
        String EMP_LNAME    =LastName.getText().trim();
        String EMP_IC       =IC.getText();
        String EMP_GENDER   =Gender.getSelectedItem().toString();
        String EMP_RACE     =Race.getSelectedItem().toString();
        String EMP_RELIGION =Religion.getSelectedItem().toString();
        String EMP_EMAIL    =Email.getText().trim();
        String EMP_CONTACT  =Contact.getText();
        String EMP_CITY     =City.getText().trim();
        String EMP_POSTCODE =PostCode.getText();
        String EMP_ADDRESS  =Address.getText().trim();
        String EMP_STATUS   =SelectedButton();

        int pwnum=0;
        String empic=EMP_IC.substring(0, 6)+EMP_IC.substring(7,9)+EMP_IC.substring(10,14);
        String pwSQL= "INSERT INTO password (PW_PASSWORD,PW_DATE) VALUES(DES_ENCRYPT('"+empic+"'),CURDATE())";
        
        if(EMP_JOBTITLE.equals("Choose") || EMP_USERNAME.isEmpty() || EMP_FNAME.isEmpty() || EMP_LNAME.isEmpty() || EMP_IC.isEmpty() || EMP_GENDER.equals("Choose")
                || EMP_RACE.equals("Choose") || EMP_RELIGION.equals("Choose") || EMP_EMAIL.isEmpty() || EMP_CONTACT.isEmpty() || EMP_CITY.isEmpty() || EMP_POSTCODE.isEmpty()
                || EMP_ADDRESS.isEmpty() || EMP_STATUS.equals("") )
        {
            JOptionPane.showMessageDialog(null, "Please fill up all information.");
        }
        else
        {
            String message = "Add a new employee profile?";
            String title = "Confirmation?";
            int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (reply == JOptionPane.YES_OPTION)
            {
                dbOB.update(pwSQL);
                rset=dbOB.getResultSet("SELECT MAX(PW_NUMBER) FROM password");
                try 
                {       if(rset.next())
                        {
                            pwnum=Integer.parseInt(rset.getString(1).toString());
                            System.out.println("num : "+pwnum);
                        }
                
                        try
                        {   Class.forName("org.gjt.mm.mysql.Driver"); }
                        catch (ClassNotFoundException ex)
                        {   Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex); }
                    
                        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/vims", "root", "123");
                        String SQL = "INSERT INTO employee VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

                        FileInputStream fis = null;
                        PreparedStatement ps = null;
                    
                        try 
                        {
                                conn.setAutoCommit(false);
                                File file = new File(PATH);
                                try {
                                    fis = new FileInputStream(file);
                                } 
                                catch (FileNotFoundException ex) {
                                    Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            
                                ps = conn.prepareStatement(SQL);

                                ps.setInt   (1 , Integer.parseInt(EMP_ID.toString()));
                                ps.setString(2 , EMP_FNAME);
                                ps.setString(3 , EMP_LNAME);
                                ps.setString(4 , EMP_IC);
                                ps.setString(5 , EMP_EMAIL);
                                ps.setString(6 , EMP_CONTACT);
                                ps.setString(7 , EMP_ADDRESS);
                                ps.setString(8 , EMP_POSTCODE);
                                ps.setString(9 , EMP_CITY);
                                ps.setString(10, EMP_JOBTITLE);
                                ps.setInt   (11, pwnum);
                                ps.setString(12, EMP_STATUS);
                                ps.setString(13, EMP_GENDER);
                                ps.setString(14, EMP_RACE);
                                ps.setString(15, EMP_RELIGION);
                                ps.setString(16, EMP_USERNAME);
                                ps.setBinaryStream(17, fis, (int) file.length());
                                
                                ps.executeUpdate();

                                conn.commit();
                     } 
                     finally 
                     {
                            ps.close();
                        try {
                            fis.close();
                        } catch (IOException ex) {
                            Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
                        }                    }
                 
                } 
                catch (SQLException ex) 
                {  System.out.println(ex.getMessage()); }                           
            }
        }
    }
    
    public void StoreImage() throws FileNotFoundException, SQLException, IOException, ClassNotFoundException
    {
        Class.forName("org.gjt.mm.mysql.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/vims", "root", "123");
        //String INSERT_PICTURE = "insert into photo1(img) values (?, ?)";        
        String INSERT_PICTURE = "UPDATE employee SET EMP_PHOTO=(?) WHERE EMP_USERNAME='"+UserName.getText()+"'";
        System.out.print(INSERT_PICTURE);
        FileInputStream fis = null;
        PreparedStatement ps = null;
        try 
        {
                conn.setAutoCommit(false);
                File file = new File(PATH);
                fis = new FileInputStream(file);
             
                ps = conn.prepareStatement(INSERT_PICTURE);
                ps.setBinaryStream(1, fis, (int) file.length());
                conn.commit();
                System.out.println("zzz");
        } 
        finally
        {
        
        ps.close();
        fis.close();
        }
    }
    
    private void DeleteEmployee()
    {
        String title = "Confirmation";
        String msg   = "Deactivate employee "+IdField.getText()+" ?";
        int reply = JOptionPane.showConfirmDialog(null, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if(reply==JOptionPane.YES_OPTION)
        {
            String SQL="UPDATE employee SET EMP_STATUS='INACTIVE' WHERE EMP_ID="+selectedID;
            if(dbOB.update(SQL)==true)
            {
                JOptionPane.showMessageDialog(null , "Employee "+IdField.getText()+" has been Deactivated.");
                this.setVisible(false);
            }
            else
                JOptionPane.showMessageDialog(null , "Failure occurs during deletion.");
        }
        else
            active.setSelected(true);
    }

    private void UpdateEmployee()
    {
        String title = "Confirmation";
        String msg   = "Update employee "+IdField.getText()+" ?";
        int reply = JOptionPane.showConfirmDialog(null, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if(reply==JOptionPane.YES_OPTION)
        {
            String SQL="UPDATE employee SET EMP_JOBTITLE='"+Position.getSelectedItem().toString()+"' WHERE EMP_ID="+selectedID;
            if(dbOB.update(SQL)==true)
            {
                JOptionPane.showMessageDialog(null , "Employee "+IdField.getText()+" has been Updated.");
                this.setVisible(false);
            }
            else
                JOptionPane.showMessageDialog(null , "Failure occurs during deletion.");
        }
        
    }
    
    
    private void ComponentDISABLED()
    {  

        Position.setEnabled(false);
        UserName.setEditable(false);
        FirstName.setEditable(false);
        LastName.setEditable(false);
        IC.setEditable(false);
        Gender.setEnabled(false);
        
        Race.setEnabled(false);
        Religion.setEnabled(false);
        Email.setEditable(false);
        Contact.setEditable(false);
        City.setEditable(false);
        PostCode.setEditable(false);
        Address.setEditable(false);
        active.setEnabled(false);
        inactive.setEnabled(false);
    }
    
    // Retrive Selected Employee Details
    void DisplayEmpInfo()
    {
        String SQL= "SELECT * FROM employee WHERE EMP_ID ="+selectedID;
        rset = dbOB.getResultSet(SQL);
        try
        {
          if(rset.next())
          {
                IdField.setText(rset.getString(1));
                FirstName.setText(rset.getString(2));
                LastName.setText(rset.getString(3));
                IC.setText(rset.getString(4));
                Email.setText(rset.getString(5));
                Contact.setText(rset.getString(6));
                UserName.setText(rset.getString(16));
                Address.setText(rset.getString(7));
                PostCode.setText(rset.getString(8));
                City.setText(rset.getString(9));
                Position.setSelectedItem(rset.getString(10));
                Gender.setSelectedItem(rset.getString(13));
                Race.setSelectedItem(rset.getString(14));
                Religion.setSelectedItem(rset.getString(15));
                String status = rset.getString(12);
                if(status.equals("ACTIVE"))
                    active.setSelected(true);
                else
                    inactive.setSelected(true);
          }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }  
    }
    
    
    // Choose photo . Contraint: When Add new employee 
    private void PhotoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PhotoMouseClicked
        
        if(action.equals("Add"))
        {
        
            FileChooser.setVisible(true);
            int returnVal = FileChooser.showOpenDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) 
            {
                File file = FileChooser.getSelectedFile();
                try 
                {
                    Photo.setText("");       
                    PATH=file.getAbsolutePath();
                    Image img = Toolkit.getDefaultToolkit().createImage(PATH).getScaledInstance(180, 180, 0);
                    ImageIcon ic = new ImageIcon(img);
                    Photo.setIcon(ic);
                } 
                catch (Exception ex) 
                {
                    System.out.println("problem accessing file"+file.getAbsolutePath());
                    Photo.setText("Click here to add photo");
                }
            }
         }
    }//GEN-LAST:event_PhotoMouseClicked
    
    private void FileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FileChooserActionPerformed
        
    }//GEN-LAST:event_FileChooserActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Employee().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel AddLabel;
    private javax.swing.JEditorPane Address;
    private javax.swing.JTextField City;
    private javax.swing.JLabel CityLabel;
    private javax.swing.JButton CloseEmpButton;
    private javax.swing.JLabel ConLabel;
    private javax.swing.JFormattedTextField Contact;
    private javax.swing.JTextField Email;
    private javax.swing.JLabel EmailLabel;
    private javax.swing.JButton EmpButton;
    private javax.swing.JFileChooser FileChooser;
    private javax.swing.JTextField FirstName;
    private javax.swing.JLabel FnLabel;
    private javax.swing.JComboBox Gender;
    private javax.swing.JLabel GenderLabel;
    private javax.swing.JFormattedTextField IC;
    private javax.swing.JLabel IcLabel;
    private javax.swing.JLabel IdField;
    private javax.swing.JTextField LastName;
    private javax.swing.JLabel LnLabel;
    private javax.swing.JLabel PcLabel;
    private javax.swing.JLabel Photo;
    private javax.swing.JLabel PosLabel;
    private javax.swing.JComboBox Position;
    private javax.swing.JFormattedTextField PostCode;
    private javax.swing.JComboBox Race;
    private javax.swing.JLabel RaceLabel;
    private javax.swing.JComboBox Religion;
    private javax.swing.JLabel ReligionLabel;
    private javax.swing.JLabel StatusLabel;
    private javax.swing.JLabel UidLabel;
    private javax.swing.JLabel UnLabel;
    private javax.swing.JTextField UserName;
    private javax.swing.JRadioButton active;
    private javax.swing.JRadioButton inactive;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
}
